/**
 * 
 */
package org.client.ui;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.client.view.manage.IMRegisterClientConServer;
import org.pojo.User;

/**
 * @author 周亮
 * @time 2012-6-17 下午07:20:03
 *
 */
public class IMRegistWnd extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @param args
	 */
	private JPanel p ;
	private JLabel lblname,lblnickname,lblpwd,lblRePwd,lblSex,lblImage,lblsign,jLabel0;
	private JTextField txtname,txtnickname;
	private JPasswordField txtpwd,txtRePwd;
	private JRadioButton rbMale,rbFemale;
	private JTextArea txtSign;
	private JRadioButton[] ckbImage = new JRadioButton[8];
	private JButton btnOK,btnCancle,btnReturn;
	String CheckedHeadImg;//选中的头像
	public IMRegistWnd()
	{
		super("用户注册");
		p = new JPanel(null);
		
		ImageIcon backgrd = new ImageIcon("images/注册背景哇.png");
		jLabel0 = new JLabel(backgrd);
		jLabel0.setBounds(0, 0, backgrd.getIconWidth(),backgrd.getIconHeight());
		this.getLayeredPane().add(jLabel0,new Integer(Integer.MIN_VALUE));
		JPanel jp = (JPanel) this.getContentPane();
		jp.setOpaque(false);
		p.setOpaque(false);
		
		lblname = new JLabel("用户名");
		lblnickname = new JLabel("昵称");
		lblpwd = new JLabel("密 码");
		lblRePwd = new JLabel("确认密码");
		lblSex = new JLabel("性别");
		lblsign = new JLabel("个性签名");
		//lblImage = new JLabel("头像");
		txtname = new JTextField(20);
		txtnickname = new  JTextField(20);
		txtpwd = new JPasswordField(20);
		txtRePwd =  new JPasswordField(20);
		rbMale = new  JRadioButton("男");
		rbMale.setSelected(true);
		rbFemale = new JRadioButton("女");
		txtSign = new JTextArea(5,10);
		JPanel pimg = new JPanel(new GridLayout(2,5));
		ButtonGroup bgimg = new ButtonGroup();
		for(int i=0;i<ckbImage.length ;i++)
		{
			ckbImage[i] = new JRadioButton(String.valueOf(i+1),new ImageIcon("images/"+(i+1)+".gif"));
			ckbImage[i].addItemListener(new ImageListener());
			bgimg.add(ckbImage[i]);
			pimg.add(ckbImage[i]);
		}
		btnOK = new JButton("确定");
		btnOK.addActionListener(this);
		btnCancle =new JButton("取消");
		btnCancle.addActionListener(this);
		btnReturn = new JButton("返回");
		btnReturn.addActionListener(this);
		ButtonGroup bgsex = new ButtonGroup();
		bgsex.add(rbMale);
		bgsex.add(rbFemale);
		lblname.setBounds(65, 30, 60, 25);
		txtname.setBounds(130,30,200,25);
		lblnickname.setBounds(65,60,60,25);
		txtnickname.setBounds(130,60,200,25);
		lblpwd.setBounds(65,90,60,25);
		txtpwd.setBounds(130,90,200, 25);
		lblRePwd.setBounds(65,120,60,25);
		txtRePwd.setBounds(130, 120, 200, 25);
		lblSex.setBounds(65, 150, 60, 25);
		rbMale.setBounds(130,150, 60, 25);
		rbFemale.setBounds(268,150,60,25);
		lblsign.setBounds(65, 180, 60, 25);
		txtSign.setBounds(130, 180,200,30);
		//lblImage.setBounds(65,210,60,25);
		pimg.setBounds(72, 220, 250, 100);
		btnOK.setBounds(100, 335, 60, 25);
		btnCancle.setBounds(165,335,60,25);
		btnReturn.setBounds(230,335,60,25);
		p.add(lblname);
		p.add(txtname);
		p.add(lblnickname);
		p.add(txtnickname);
		p.add(lblpwd);
		p.add(txtpwd);
		p.add(lblsign);
		p.add(txtSign);
		//p.add(lblImage);
		p.add(lblRePwd);
		p.add(txtRePwd);
		p.add(lblSex);
		p.add(rbMale);
		p.add(rbFemale);
		p.add(pimg);
		p.add(btnOK);
		p.add(btnCancle);
		p.add(btnReturn);
		this.add(p);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/im.png"));
		this.setSize(400, 400);
		this.setLocation(300, 300);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IMRegistWnd ui = new IMRegistWnd();
	}
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if(source == btnOK)
		{
			btnOKClick();
		}
		if(source == btnCancle)
		{
			btnCancleClick();
		}
		if(source == btnReturn)
		{
			btnRetuenClick();
		}
	}
	private void btnOKClick()
	{
		String  username = txtname.getText();
		String nickname = txtnickname.getText();
		String userpwd = new String(txtpwd.getPassword());
		String repwd = new String(txtRePwd.getPassword());
		String sign = txtSign.getText();
		String sex;
		if(rbFemale.isSelected())
		{
			sex = "男";
		}
		else
		{
			sex = "女";
		}
		if(username.equals(""))
		{
			JOptionPane.showMessageDialog(btnOK, "用户名不能为空","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(nickname.equals(""))
		{
			JOptionPane.showMessageDialog(btnOK, "昵称不能为空","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(userpwd.equals(""))
		{
			JOptionPane.showMessageDialog(btnOK, "密码不能为空","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(userpwd.length()<6||userpwd.length()>10)
		{
			JOptionPane.showMessageDialog(btnOK, "密码长度范围在6-10之间","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(repwd.length()<6 || repwd.length()>10)
		{
			JOptionPane.showMessageDialog(btnOK, "确认密码长度范围在6-10之间","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(!repwd.equals(userpwd))
		{
			JOptionPane.showMessageDialog(btnOK, "确认密码与密码必须相同","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
			//注册提交
			User u = new User(username,nickname,userpwd,sex,sign,CheckedHeadImg);
			boolean b = new IMRegisterClientConServer().sendRegisterInfoToServer(u);
			if(b){
				JOptionPane.showMessageDialog(btnOK, "您的帐号已注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
				this.dispose();
			}
			else JOptionPane.showMessageDialog(btnOK, "您的帐号未成功注册！", "失败", JOptionPane.INFORMATION_MESSAGE);
	}
	private void btnCancleClick()
	{
		txtname.setText("");
		txtnickname.setText("");
		txtpwd.setText("");
		txtRePwd.setText("");
		rbMale.setSelected(true);
	}
	private void btnRetuenClick()
	{
		this.dispose();
		//回到login界面
	}
	class ImageListener implements ItemListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
		 */
		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			JRadioButton source = (JRadioButton) e.getSource();
			if(source.isSelected())
			{
				source.setBorderPainted(true);
				CheckedHeadImg  = source.getText()+".gif";
				System.out.println(CheckedHeadImg);
			}else
			{
				source.setBorderPainted(false);
			}
		}
		
	}
}
